package com.example.javamaday.leetcode;

import java.util.Comparator;
import java.util.PriorityQueue;

public class LeetCodeTest1046 {

    public static void main(String[] args) {
        new LeetCodeTest1046().lastStoneWeight(new int[]{2, 7, 4, 1, 8, 1});
    }


    public int lastStoneWeight(int[] stones) {

        PriorityQueue<Integer> priorityQueue = new PriorityQueue<Integer>(stones.length, new Comparator<Integer>() {
            @Override
            public int compare(Integer v1, Integer v2) {
                return -Integer.compare(v1, v2);
            }
        });
        for (int i = 0; i < stones.length; i++) {
            System.out.println("offer - " + stones[i]);
            priorityQueue.offer(stones[i]);
        }
        while (priorityQueue.size() > 1) {
            int num = Math.abs(priorityQueue.poll() - priorityQueue.poll());
            if (num != 0) {
                priorityQueue.offer(num);
            }
        }
        if (priorityQueue.size() == 0) {
            return 0;
        } else {
            return priorityQueue.poll();
        }
    }
}
